﻿using System;
using System.Data;
using System.IO;
using System.Windows.Forms;


namespace CmdPrep.Backends
{
    public class CmdBackend
    {
        #region Private static methods
        
        private static string getExistingDirectory(string directoryName)
        {
            string nonEmptyDirectoryName = string.IsNullOrEmpty(directoryName)
                ? Path.GetTempPath()
                : directoryName;
            
            DirectoryInfo directoryInfo = new DirectoryInfo(nonEmptyDirectoryName);
            
            return directoryInfo.Exists 
                ? directoryInfo.FullName 
                : getExistingDirectory(directoryInfo.Parent.FullName);
        }
        
        #endregion

        #region Public methods

        public static DataTable getCmdDataTable(string projectsContentDirectory, string partialProjectPath)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(projectsContentDirectory + @"\" + partialProjectPath);

            DataTable dataTable = new DataTable("CmdFiles");
            dataTable.Columns.Add(new DataColumn("fileName", Type.GetType("System.String")));
            dataTable.Columns.Add(new DataColumn("fullName", Type.GetType("System.String")));
            
            if (directoryInfo.Exists)
            {
                foreach (FileInfo cmdFile in directoryInfo.GetFiles("*.CMD")) 
                {
                    DataRow dataRow = dataTable.NewRow();
                    dataRow["fileName"] = cmdFile.Name;
                    dataRow["fullName"] = cmdFile.FullName;
                    dataTable.Rows.Add(dataRow);
                }
            }
            
            return dataTable;
        }
        
        public static void deleteCmdFile(string fullName)
        {
            FileInfo fileInfo = new FileInfo(fullName);
            
            if (fileInfo.Exists)
            {
                fileInfo.Delete();

                MessageBox.Show("File '" + fullName + "' deleted.",
                    "CmdPrep - File delete done", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Information);
            }
            else 
            {
                MessageBox.Show("File '" + fullName + "' not found.",
                    "CmdPrep - File delete failed", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Asterisk);                
            }
        }
        
        public static string getInputCmdFileFromUser(string projectsContentDir, string partialProjectPath)
        {
            projectsContentDir              = @"D:" ;            // oterwise, it will pont to P:\ELSA\PROJ +\more....
            OpenFileDialog openFileDialog   = new OpenFileDialog();
            openFileDialog.DefaultExt       = "cmd";
            openFileDialog.Filter           = "Command Files|*.cmd";
            openFileDialog.InitialDirectory = getExistingDirectory(projectsContentDir + partialProjectPath);
            //openFileDialog.InitialDirectory = getExistingDirectory(projectsContentDir + @"\" + partialProjectPath);
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Title            = "Open Command File";

            openFileDialog.ShowDialog();
            
            return openFileDialog.FileName;                
        }
        
        public static string getOutputCmdFileFromUser(string projectsContentDir, string fileNameHint)
        {
            SaveFileDialog saveFileDialog   = new SaveFileDialog();
            saveFileDialog.DefaultExt       = "cmd";
            saveFileDialog.Filter           = "Command Files|*.cmd";
            saveFileDialog.InitialDirectory = @"D:\PROSJEKT\" ;         //projectsContentDir;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title            = "Save Command File";
            saveFileDialog.FileName         = fileNameHint;

            saveFileDialog.ShowDialog();
            
            return saveFileDialog.FileName;                
        }

        #endregion
    }
}
